<template>
  <el-container class="home-container">
    <el-header class="home-header">
      <div><span>电商后台管理系统</span></div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="changeWidth + 'px'"
        ><div class="toggle-button" @click="toggle">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="Path"
        >
          <!-- index只接受字符串 -->
          <el-submenu
            :index="item.id.toString()"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- /+itemT.path为其子路由跳转的地址 -->
            <el-menu-item
              :index="'/' + itemT.path"
              v-for="itemT in item.children"
              :key="itemT.id"
              @click="save('/' + itemT.path)"
              ><template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ itemT.authName }}</span>
              </template></el-menu-item
            >
          </el-submenu>
        </el-menu></el-aside
      >
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconObj: {
        125: "el-icon-user-solid",
        103: "el-icon-s-tools",
        101: "el-icon-s-goods",
        102: "el-icon-s-claim",
        145: "el-icon-s-ticket",
      },
      isCollapse: false,
      changeWidth: 200,
      Path: "",
    };
  },
  created() {
    this.getMenuList();
    this.Path = sessionStorage.getItem("Path");
  },
  methods: {
    logout() {
      sessionStorage.clear();
      this.$router.replace("/login");
    },
    async getMenuList() {
      let { data: res } = await this.$http.get("menus");
      this.menuList = res.data;
      // console.log(res);
      // console.log(typeof res.data[0].id);
    },
    toggle() {
      this.isCollapse = !this.isCollapse;
      if (this.isCollapse == false) {
        this.changeWidth = 200;
      } else {
        this.changeWidth = 64;
      }
    },
    save(a) {
      sessionStorage.setItem("Path", a);
      this.Path = a;
    },
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
}
.home-container {
  height: 100%;
}
.home-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  font-size: 21px;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background-color: #4a5064;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  color: white;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
